<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>xtal_test.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=h1>%&nbsp;This&nbsp;script&nbsp;tests&nbsp;functionality&nbsp;of&nbsp;the&nbsp;XTAL&nbsp;interface.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;The&nbsp;script&nbsp;runs&nbsp;regression&nbsp;methods&nbsp;implemented&nbsp;in&nbsp;the&nbsp;XTAL&nbsp;</span><br>
<span class=help>%&nbsp;package&nbsp;on&nbsp;a&nbsp;test&nbsp;problem&nbsp;and&nbsp;visualizes&nbsp;obtained&nbsp;results.</span><br>
<span class=help>%</span><br>
<hr>
<span class=comment>%&nbsp;load&nbsp;training/testing&nbsp;data</span><br>
<span class=comment>%-----------------------------------------</span><br>
<br>
tmp&nbsp;=load(<span class=quotes>'train.txt'</span>,<span class=quotes>'-ascii'</span>);<br>
trn_data.X&nbsp;=&nbsp;tmp(:,1)';<br>
trn_data.y&nbsp;=&nbsp;tmp(:,2);<br>
<br>
tmp&nbsp;=load(<span class=quotes>'test.txt'</span>,<span class=quotes>'-ascii'</span>);<br>
tst_data.X&nbsp;=&nbsp;tmp(:,1)';<br>
tst_data.y&nbsp;=&nbsp;tmp(:,2);<br>
<br>
<span class=comment>%&nbsp;experiment&nbsp;setup</span><br>
<span class=comment>%-----------------------------------------</span><br>
<br>
exp(1).method&nbsp;=&nbsp;<span class=quotes>'ANN1'</span>;<br>
exp(1).params&nbsp;=&nbsp;[&nbsp;5;&nbsp;10;&nbsp;20;&nbsp;40;&nbsp;100];<br>
<br>
exp(2).method&nbsp;=&nbsp;<span class=quotes>'MRS1'</span>;<br>
exp(2).params&nbsp;=&nbsp;[[100,0];&nbsp;[100,2];&nbsp;[100,5];&nbsp;[100,9];&nbsp;[200,0];&nbsp;[200,2];&nbsp;[200,5];&nbsp;[200,9]];<br>
<br>
exp(3).method&nbsp;=&nbsp;<span class=quotes>'PPR1'</span>;<br>
exp(3).params&nbsp;=&nbsp;[1;&nbsp;2;&nbsp;5;&nbsp;8;&nbsp;10;&nbsp;20;&nbsp;50];<br>
<br>
exp(4).method&nbsp;=&nbsp;<span class=quotes>'CTM1'</span>;<br>
exp(4).params&nbsp;=&nbsp;[[2,0];&nbsp;[2,2];&nbsp;[2,5];&nbsp;[2,9];&nbsp;[3,0];&nbsp;[3,2];&nbsp;[3,5];&nbsp;[3,9]];<br>
<br>
exp(5).method&nbsp;=&nbsp;<span class=quotes>'KNN1'</span>;<br>
exp(5).params&nbsp;=&nbsp;[2;&nbsp;3;&nbsp;4;&nbsp;5;&nbsp;6;&nbsp;7;&nbsp;8;&nbsp;9;&nbsp;10;&nbsp;11];<br>
<br>
<span class=comment>%-----------------------------------------</span><br>
<br>
<span class=keyword>for</span>&nbsp;i=1:length(exp)<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'\nExperiment:&nbsp;%s\n'</span>,&nbsp;exp(i).method);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'---------------------------------------\n'</span>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;[ypred,exp(i).nrms,exp(i).rms0,exp(i).nmin]&nbsp;=&nbsp;xtal(trn_data,tst_data,exp(i).method,exp(i).params);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;[exp(i).min_nrms,exp(i).best_param_idx]&nbsp;=&nbsp;min(exp(i).nrms);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Minimal&nbsp;NRMS=%f&nbsp;achieved&nbsp;with&nbsp;parameter&nbsp;(&nbsp;'</span>,&nbsp;exp(i).min_nrms);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'%d&nbsp;'</span>,&nbsp;exp(i).params(exp(i).best_param_idx,:));<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>')\n'</span>);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;h0=<span class=graph>figure</span>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=graph>set</span>(h0,<span class=quotes>'Name'</span>,exp(i).method);<br>
&nbsp;&nbsp;&nbsp;&nbsp;h1=<span class=graph>plot</span>(trn_data.X,trn_data.y,<span class=quotes>'kx'</span>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;hold&nbsp;on;<br>
&nbsp;&nbsp;&nbsp;&nbsp;h2=<span class=graph>plot</span>(tst_data.X,tst_data.y,<span class=quotes>'rx'</span>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;h3=<span class=graph>plot</span>(tst_data.X,ypred(exp(i).best_param_idx,:),<span class=quotes>'bx--'</span>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;legend([h1&nbsp;h2&nbsp;h3],<span class=quotes>'traing&nbsp;examples'</span>,<span class=quotes>'testing&nbsp;examples'</span>,<span class=quotes>'prediction'</span>);<br>
<span class=keyword>end</span><br>
<br>
<br>
[min_nrms,best_method]&nbsp;=&nbsp;min([exp.min_nrms]);<br>
<span class=io>fprintf</span>(<span class=quotes>'\n**&nbsp;Summary&nbsp;**\n\n'</span>);<br>
<span class=io>fprintf</span>(<span class=quotes>'Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NRMS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NMIN&nbsp;&nbsp;parameter\n'</span>);<br>
<span class=io>fprintf</span>(<span class=quotes>'--------------------------------------------------\n'</span>);<br>
<span class=keyword>for</span>&nbsp;i=1:length(exp)<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'%s&nbsp;&nbsp;&nbsp;%12.6f&nbsp;%12.6f&nbsp;&nbsp;(&nbsp;'</span>,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exp(i).method,exp(i).min_nrms,exp(i).nmin(exp(i).best_param_idx));<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'%d&nbsp;'</span>,&nbsp;exp(i).params(exp(i).best_param_idx,:));<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>')'</span>);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;i&nbsp;==&nbsp;best_method<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;best&nbsp;result'</span>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'\n'</span>);<br>
<span class=keyword>end</span><br>
</code>
